from flask import Blueprint, jsonify

from models import News, Category, Share, ImgThum

photo_bp = Blueprint('photo_bp', __name__)


@photo_bp.route('/categorys')
def get_categorys():
    data = {
        "categorys": [],
        "meta": {
            "msg": "success",
            "status": 200
        }
    }
    cates = Category.query.all()
    cates = [{
        "id": c.id,
        "name": c.name,
        "create_time": c.create_time,
        "update_time": c.update_time
    } for c in cates]
    data['categorys'] = cates
    return jsonify(data)


# 获取图文分享列表数据
@photo_bp.route('/<int:cid>')
def get_phones(cid):
    data = {
        "data": [],
        "meta": {
            "msg": "success",
            "status": 200
        }
    }
    if cid == 0:
        result = Share.query.all()
    else:
        result = Share.query.filter_by(channel_id=cid).all()
    result = [{
        "id": r.id,
        "title": r.title,
        "img_url": r.img_url,
        "summary": r.summary
    } for r in result]
    data['data'] = result
    return jsonify(data)


# 获取图文分享详情数据
@photo_bp.route('/detail/<int:pid>')
def get_photo_detail(pid):
    data = {
        "detail": {},
        "meta": {
            "msg": "success",
            "status": 200
        }
    }
    r = Share.query.filter_by(id=pid).first()
    result = {
        "id": r.id,
        "title": r.title,
        "img_url": r.img_url,
        "summary": r.summary,
        "content": r.content,
        "view": r.view,
        "create_time": r.create_time
    }
    data['detail'] = result
    return jsonify(data)


# 获取图文分享详情数据的缩略图
@photo_bp.route('/thumb/<int:pid>')
def get_photo_detail_thumb(pid):
    data = {
        "data": [],
        "meta": {
            "msg": "success",
            "status": 200
        }
    }
    result= ImgThum.query.filter_by(pid=pid).all()
    result = [{
        "id": r.id,
        "src": r.src,
        "pid": r.pid
    } for r in result]
    print(result)
    data['data'] = result
    return jsonify(data)
